package com.ishow.bs.controller.example;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ishow.bs.controller.common.BaseController;
import com.ishow.bs.utils.BusinessMessageInfo;
import com.ishow.common.constant.ConstantEnum;
import com.ishow.common.utils.RedisUtil;
import com.ishow.core.entity.ExampleEntity;
import com.ishow.core.service.example.ExampleBizService;

@Controller
@Scope("singleton") // 1、为了性能。2、不需要多例。只要controller中不定义属性，那么单例完全是安全的。
@RequestMapping("/")
public class ExampleController extends BaseController {
	@Resource(name = "exampleBizService")
	private ExampleBizService exampleBizService;

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@RequestMapping("index.do")
	public String index(HttpServletRequest request) {
		request.getSession().setAttribute("test", "aaa");

		System.out.println(request.getSession().getAttribute("test"));
		RedisUtil.addValue("test", "bbb", 7200000);

		System.out.println(RedisUtil.getValue("test"));
		return "index.jsp";
	}

	@RequestMapping("example.do")
	public String example() {
		return "example/example.jsp";
	}

	@RequestMapping("example_edit.do")
	@ResponseBody
	public List<ExampleEntity> exampleEdit(Integer id, HttpServletRequest request) {
		List<ExampleEntity> exampleEntity = null;
		if (id != null) {
			exampleEntity = exampleBizService.selectList(null);
			request.setAttribute("exampleEntity", exampleEntity);
		}
		// return "example/example_edit_frm.jsp";
		return exampleEntity;
	}

	@ResponseBody
	@RequestMapping(value = "/insert.do")
	public JSONObject insertExample(HttpSession session, HttpServletRequest request, ExampleEntity exampleEntity) {
		return insertOrUpdateExample(session, request, exampleEntity);
	}

	@ResponseBody
	@RequestMapping(value = "/update.do")
	public JSONObject updateExample(HttpSession session, HttpServletRequest request, ExampleEntity exampleEntity) {
		return insertOrUpdateExample(session, request, exampleEntity);
	}

	private JSONObject insertOrUpdateExample(HttpSession session, HttpServletRequest request,
			ExampleEntity exampleEntity) {
		try {
			if (exampleEntity == null || exampleEntity.getId() == null) {
				exampleEntity = new ExampleEntity();
				exampleEntity.setName("chichi");
				exampleEntity.setCreateTime(new Date());
				exampleEntity.setSex("男");
				exampleEntity.setStatus(ConstantEnum.CommonStatus.ENABELED.getCode());
				exampleBizService.insertOrUpdate(exampleEntity);
				return BusinessMessageInfo.SUCCESS;
			} else {
				// 修改
				// 把禁止修改的置null，xml里就不会生成对应的修改sql.
				exampleEntity.setCreateTime(null);
				exampleBizService.updateAllColumnById(exampleEntity);
				return BusinessMessageInfo.SUCCESS;
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			// 未知错误
			return BusinessMessageInfo.SYS_IS_BUSY;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/getEntityById.do")
	public ExampleEntity getEntityById(Integer id, HttpServletRequest request){
		return exampleBizService.getEntityById(id);
	}

}
